

// always requires an idle view 
#include "kb.h"
#include "touch_algorithm.h"
#include "custom_event.h"
#include "gui.h"

#include "views.h"
#include "views_util.h"

#include "xapp_config.h"
#include "xapp_ble.h"
#include "xapp_clock.h"
#include "xapp_exercise.h"
#if USER_CHECK_ENABLED( PEDOMETER )
#include "xapp_pedometer.h"
#endif 
#include "xapp_profile.h"
#include "xapp_exercise.h"
#include "xapp_localize.h"

#include "algorithm.h"

#include "log.h"
#define LOG     LOG_INFO0

enum tagWatchface
{
    tagWatchfaceDragStartX,
    tagWatchfaceDragStartY,

    tagWatchfaceToLeft,
    tagWatchfaceToRight,
    tagWatchfaceToTop,
    tagWatchfaceToBottom,
    tagWatchfacePreview,
    tagWatchfaceTurnOff,
};

GUI_VIEW_FUNC_DECLARE_ALL;

static uint8_t string_calorie[6];
static uint8_t string_steps[6];
static uint8_t string_date[6];
const static ui_widgets_type_t wglWatchface_12[] = 
{
    // background 
    [0] = uiCanvas( 0, 0, 240, 240, COLOR_BLACK ),
    // hour shi
    [1] = uiDigital( IMG_Watchface_12_OrangeDigital_033_047_10, 27, 59, 33, 47 ),
    // hour ge
    [2] = uiDigital( IMG_Watchface_12_OrangeDigital_033_047_10, 60, 59, 33, 47 ),
    // minute shi
    [3] = uiDigital( IMG_Watchface_12_OrangeDigital_033_047_10, 54, 130, 33, 47 ),
    // minute ge
    [4] = uiDigital( IMG_Watchface_12_OrangeDigital_033_047_10, 87, 130, 33, 47 ),

    // calorie icon 
    [5] = uiPictureBox( IMG_Watchface_12_Icon_015_019_02, 0, 109, 97, 15, 19 ),
    // step icon 
    [6] = uiPictureBox( IMG_Watchface_12_Icon_015_019_02, 1, 109, 67, 15, 19 ),
    // calorie value
    [7] = uiNumberWithBuffer( 134, 67, 100, 20, COLOR_BLACK, COLOR_WHITE, ALIGN_LEFT_TOP, FILL_NONE, ENCODING_ASCII, FONT_ASCII_0_20_20, NULL, string_calorie, 5),
    // step value 
    [8] = uiNumberWithBuffer( 134, 97, 100, 20, COLOR_BLACK, COLOR_WHITE, ALIGN_LEFT_TOP, FILL_NONE, ENCODING_ASCII, FONT_ASCII_0_20_20, NULL, string_steps, 5),
    // date 
    [9] = uiNumberlistWithBuffer( 134, 138, 100, 20, COLOR_BLACK, COLOR_ORANGE, ALIGN_LEFT_TOP, FILL_NONE, ENCODING_ASCII, FONT_ASCII_0_20_20, "%02d/%02d", string_date, 5),
    // weekday 
    [10] = uiTextbox( 134, 162, 100, 20, COLOR_BLACK, COLOR_ORANGE, ALIGN_LEFT_TOP, FILL_NONE, ENCODING_UNICODE, FONT_ASCII_0_20_20 ),
};


GUI_VIEW_DEF_NR( viewWatchface12 ) = 
{
    // name 
    .name = "Watchface12",
    
    // init 
    .init = initialize,
    .display_func = display_callback_func,
    .refresh_func_check = display_refresh_func,
    .destroy_func = on_destroy_func,
    .set_parent_func = on_set_parent_func, 
    
    // input 
    .on_touch = on_touch,
    .on_button = on_button,
    .on_event_update = on_event_update,     
    
    .pWidgetlist =  (ui_widgets_type_t*)wglWatchface_12 ,
    .widget_cnt = WIDGET_COUNT( wglWatchface_12 ),

    .flag = GUI_VIEW_FLAG_FUNC | GUI_VIEW_FLAG_ALIVE_DELAY
};

//static uint32_t m_idle_timestamp;

/* 当前view初始化
// 需要指定前使用的控件组
// 自动更新间隔，不使用自动更新间隔则interval = 0

*/
static void initialize(gui_menu_t * menu, void* pvParameter)
{
    menu->interval = 1000;
    const profile_system_t* p_profile_system = xapp_profile_get_system();
    menu->lifetime = p_profile_system->time_of_light_on * 1000;
}
static void on_set_parent_func(gui_menu_t * menu, const gui_view_t * parent)
{
//    p_this_parent = parent;
}
/* 销毁函数
// 在销毁前处理当前信息，比如不允许销毁时，将当前界面状态保存为background。
// true:  允许销毁
// false: 不允许自动销毁，则GUI只关闭当前显示，输入事件仍由当前view进行处理。
          那么view判断当前状态为background时，作出对应background下的按键、触摸等事件反馈
*/
static uint32_t on_destroy_func(gui_menu_t * menu)
{
        
    return GUI_DESTROY_RET_DEFAULT; 
}

/* 显示回调
   重新指定参数, 用来获取当前界面的控件组
   ** 当前界面有多个控件组时，应该有条件确定使用哪一组控件组 ** 
*/
static void display_callback_func(gui_menu_t * menu)
{
    ui_property_t prop;
    ui_widgets_type_t * widgets = menu->widgets;
    const profile_system_t* p_sys_desc = xapp_profile_get_system();
    uint32_t steps;
    #if USER_CHECK_ENABLED( PEDOMETER )
    const xapp_pedo_desc_t* p_pedo_desc = xapp_pedometer_get_desc();
    steps = p_pedo_desc->steps;
    #endif 


    const datetime_t* pdt = xapp_clock_get_now();
    string_t const *s;

    uint8_t hour = pdt->hour;
    uint8_t minute = pdt->minute;
//    uint8_t second = pdt->second;
    if( p_sys_desc->time_format > 0 && hour > 12 )
    {
        hour -= 12;
    }
    PROP_ASSIGN_DIGITAL( prop, hour / 10 );
    PROP_UPDATE_AT_NO( 1 );
    PROP_ASSIGN_DIGITAL( prop, hour % 10 );
    PROP_UPDATE_AT_NO( 2 );
    PROP_ASSIGN_DIGITAL( prop, minute / 10 );
    PROP_UPDATE_AT_NO( 3 );
    PROP_ASSIGN_DIGITAL( prop, minute % 10 );
    PROP_UPDATE_AT_NO( 4 );

    PROP_ASSIGN_NUMBER( prop, algo_lib_pedo_get_calorie( steps ) );
    PROP_UPDATE_AT_NO( 7 );

    PROP_ASSIGN_NUMBER( prop, steps );
    PROP_UPDATE_AT_NO( 8 );

    PROP_ASSIGN_NUMBERLIST_2( prop, pdt->month, pdt->day );
    PROP_UPDATE_AT_NO( 9 );

    s = (string_t*)xapp_local_get_english_weekday_simple( pdt->weekday );
    PROP_ASSIGN_STRING( prop, s );
    PROP_UPDATE_AT_NO( 10 );
}

/* 刷新显示回调
   根据当前状态，重新绘制指定的控件组 
*/
static int display_refresh_func(gui_menu_t * menu)
{
    return 1;
}
static void on_touch(void* pvParameters, gui_menu_t * menu)
{
    wf_on_touch( &viewWatchface12, pvParameters, menu );
    // tp_algo_evt_t* touch = (tp_algo_evt_t*)pvParameters;
    // switch( touch->type )
    // {
    //     case TP_ALGO_EVT_TYPE_CLICK:
    //     break;

    //     case TP_ALGO_EVT_TYPE_SHORT_READY:
    //     local_view_event_handler( tagWatchfacePreview );
    //     break;


    //     case TP_ALGO_EVT_TYPE_DRAG_X:
    //     VALUES_SET( x, touch->deltaX );
    //     local_view_event_handler( tagWatchfaceDragStartX );
    //     break;

    //     case TP_ALGO_EVT_TYPE_DRAG_Y:
    //     VALUES_SET( y, touch->deltaY );
    //     local_view_event_handler( tagWatchfaceDragStartY );        
    //     break;

    //     case TP_ALGO_EVT_TYPE_SLIDE_U:
    //     local_view_event_handler( tagWatchfaceToTop );
    //     break;

    //     case TP_ALGO_EVT_TYPE_SLIDE_D:
    //     local_view_event_handler( tagWatchfaceToBottom );
    //     break;

    //     case TP_ALGO_EVT_TYPE_SLIDE_L:
    //     local_view_event_handler( tagWatchfaceToLeft );
    //     break;

    //     case TP_ALGO_EVT_TYPE_SLIDE_R:
    //     local_view_event_handler( tagWatchfaceToRight );
    //     break;

    //     default:
    //     break;
    // }
}
/* 界面的按键处理

*/
static void on_button(void* pvParameters, gui_menu_t * menu)
{
    wf_on_button( &viewWatchface12, pvParameters, menu );
    // kb_evt_t* kb = (kb_evt_t*)pvParameters;

    // switch(kb->type)
    // {
    //     case KB_EVT_CLICK:
    //     local_view_event_handler( tagWatchfaceTurnOff );
    //     break;
    // }
}

static void on_event_update(void * pvParameters, gui_menu_t * menu)
{
    wf_on_event_update( &viewWatchface12, pvParameters, menu );
    // // example for custom event update 
    // custom_evt_t* evt = (custom_evt_t*)pvParameters;
    // switch( evt->type )
    // {
    //     case CUSTOM_EVT_TYPE_WATCHFACE_UPDATE: // reload watchface 
    //     gui_view_create( views_load_current_watchface() );
    //     break;

    //     default:
    //     break;
    // }
}
